import java.util.Arrays;

/**
 * 443题
 */
public class CompressString {
    public static void main(String[] args) {
        char[] permutation1 = {'a','a','b','b','c','c','c'};//返回 6 ，输入数组的前 6 个字符应该是：["a","2","b","2","c","3"]
        char[] permutation2 = {'a'};//返回 1 ，输入数组的前 1 个字符应该是：["a"]
        char[] permutation3 = {'a','b','b','b','b','b','b','b','b','b','b','b','b'};//返回 4 ，输入数组的前 4 个字符应该是：["a","b","1","2"]。
        char[] permutation4 = {'a','a'};//
        char[] permutation5 = {'a','b','c'};//

        CompressString compressString = new CompressString();

        System.out.println("length:" + compressString.compressString(permutation1));
        System.out.println("length:" + compressString.compressString(permutation2));
        System.out.println("length:" + compressString.compressString(permutation3));
        System.out.println("length:" + compressString.compressString(permutation4));
        System.out.println("length:" + compressString.compressString(permutation5));

        printArray(permutation1);
        printArray(permutation2);
        printArray(permutation3);
        printArray(permutation4);
        printArray(permutation5);
    }

    private int compressString(char[] chars) {
        if(chars.length <=1){
            return chars.length;
        }
        int slow = 0;
        int fast = 0;
        int count = 0;
        while(fast <  chars.length){
            if(chars[slow] ==(chars[fast])){
                count++;
                if(fast ==  chars.length-1){
                    if(count!=1){
                        String countStr = String.valueOf(count);
                        int length = countStr.length();

                        for (int i = 0; i < length; i++) {
                            slow++;
                            chars[slow] = countStr.charAt(i);
                        }
                    }
                    slow++;
                }
                fast ++;
            }else{
                if(count==0){
                    slow ++;
                    fast ++;

                }else{
                    /**
                     * slow 写数字 然后重新定位
                     * fast重新定位
                     */
                    if(count!=1){
                        String countStr = String.valueOf(count);
                        int length = countStr.length();

                        for (int i = 0; i < length; i++) {
                            slow++;
                            chars[slow] = countStr.charAt(i);
                        }
                    }
                    slow++;
                    chars[slow] = chars[fast];
                }
                count =0;
            }
        }
        return slow;
    }

    private static void printArray(char[] permutation) {
        System.out.println("结果："+ Arrays.toString(permutation));
    }
    public int compress(char[] chars) {
        int n = chars.length;
        int write = 0, left = 0;
        for (int read = 0; read < n; read++) {
            if (read == n - 1 || chars[read] != chars[read + 1]) {
                chars[write++] = chars[read];
                int num = read - left + 1;
                if (num > 1) {
                    int anchor = write;
                    while (num > 0) {
                        chars[write++] = (char) (num % 10 + '0');
                        num /= 10;
                    }
                    reverse(chars, anchor, write - 1);
                }
                left = read + 1;
            }
        }
        return write;
    }

    public void reverse(char[] chars, int left, int right) {
        while (left < right) {
            char temp = chars[left];
            chars[left] = chars[right];
            chars[right] = temp;
            left++;
            right--;
        }
    }
}
